package com.example.administrator.liveapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.utils.AppUrl;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

/**
 * 首页广告轮播跳转webView
 */

public class AdvertisingWebView extends AppCompatActivity implements View.OnClickListener{

    WebView webView;
    String link;
    String title;
    String image;
    String describe;
    private ImageView share;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advertising_web_view);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        link = intent.getStringExtra("link");
        title = intent.getStringExtra("title");
        image = intent.getStringExtra("image");
        describe = intent.getStringExtra("describe");
        webView = (WebView) findViewById(R.id.webview);
        share = (ImageView) findViewById(R.id.share);
        share.setOnClickListener(this);
        LoadWebView();
    }

    private void LoadWebView() {
        // 设置支持javascript
        webView.getSettings().setJavaScriptEnabled(true);

        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

        // 开启 localStorage
        webView.getSettings().setDefaultTextEncodingName("utf-8");
        webView.getSettings().setDomStorageEnabled(true);
        // 启动缓存
        webView.getSettings().setAppCacheEnabled(true);
        // 设置缓存模式
        webView.requestFocus();
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        String webUrl = link;
        webView.loadUrl(webUrl);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.share://活动分享
                UMWeb web = new UMWeb(link);
                String ImageUrl="";
                ImageUrl = image.substring(1, image.indexOf("|"));
                UMImage image = new UMImage(AdvertisingWebView.this, AppUrl.URL + ImageUrl);//资源文件
                UMImage thumb = new UMImage(this, AppUrl.URL + ImageUrl);
                image.setThumb(thumb);
                web.setThumb(thumb);  //缩略图
                web.setTitle(title);//标题
                web.setDescription(describe);//描述

                //带面板文本分享
                new ShareAction(AdvertisingWebView.this)
                        .withMedia(web)
                        .setDisplayList(SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.WEIXIN ,SHARE_MEDIA.SINA, SHARE_MEDIA.QQ)
                        .setCallback(shareListener)
                        .open();
                break;
        }
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(AdvertisingWebView.this, "分享成功", Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(AdvertisingWebView.this, "分享失败" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(AdvertisingWebView.this, "分享取消", Toast.LENGTH_LONG).show();

        }
    };

    /**
     * 分享是否同意的回调
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {

    }
}
